﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using LLoger;
using LimitatorInterfaces;
using MicexMoneyFileLoader;

namespace LoadServices
{

    
    public class MicexMoneyFileLoaderService : ServiceBase, ILoadService
    {
        private string _limitFile = "";

        public MicexMoneyFileLoaderService(string configPath, ILogWriter logWriter)
            : base(configPath, logWriter)
        {
            _limitFile = (string)GetSettingsValue(Resources.LimitFilePath);
        }


        public void LoadLimits(List<LimitEntity> sourceLimitTable)
        {
            var file = LoadLimitsFile(_limitFile);

            foreach (var str in file)
            {
                var lim = ParceTextLimit(str);

                if (lim == null)
                    continue;

                sourceLimitTable.Add(lim);
            }
        }

        private LimitEntity ParceTextLimit(string str)
        {
            return null;


        }

        private string GetLimitValue(string s, string param)
        {
            string[] val = new string[2];
            
            if (s.StartsWith(param))
                 val = s.Split('=');


            return val[1];
        }

        private IEnumerable<string> LoadLimitsFile(string limitFile)
        {
            try
            {
                if(!File.Exists(limitFile))
                    throw new FileNotFoundException();

                var res = File.ReadAllLines(limitFile);

                if(res.Length < 1)
                    throw new FileLoadException("Файл пуст");
            }
            catch (Exception ex)
            {
                LogWriter.WriteLine(ex, "Не удалось открыть файл лимитов " + _limitFile);
                throw;
            }

            throw new NotImplementedException();
        }
    }
}
